
package com.hlkj.pay.app.merchant.ext.impl.alipayZft;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.stereotype.Repository;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeRoyaltyRelationBindModel;
import com.alipay.api.domain.AlipayTradeRoyaltyRelationUnbindModel;
import com.alipay.api.domain.RoyaltyEntity;
import com.alipay.api.request.AlipayTradeRoyaltyRelationBindRequest;
import com.alipay.api.request.AlipayTradeRoyaltyRelationUnbindRequest;
import com.alipay.api.response.AlipayTradeRoyaltyRelationBindResponse;
import com.alipay.api.response.AlipayTradeRoyaltyRelationUnbindResponse;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantProfitAccountExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.profit.ChannelProfitAccountBindContext;
import com.hlkj.pay.app.merchant.ext.dto.profit.ChannelProfitAccountContext;
import com.hlkj.pay.app.merchant.ext.impl.alipay.dto.AlipayConfig;
import com.hlkj.pay.app.merchant.ext.impl.alipay.handler.AliPayCommonParamsHandle;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.merchant.fund.MerchantFundAccountBindDO;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2025/01/09 9:55
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_ALI_PAY_ZFT)
public class AlipayZftProfitAccountExtPt implements ChannelMerchantProfitAccountExtPt {

    private final AliPayCommonParamsHandle aliPayCommonParamsHandle;

    @Override
    public void submitProfitAccount(ChannelProfitAccountContext channelProfitAccountContext) {

    }

    @Override
    public void updateProfitAccount(ChannelProfitAccountContext channelProfitAccountContext) {
    }

    @Override
    public void bindProfitAccount(ChannelProfitAccountBindContext channelProfitAccountBindContext) {
        log.info("支付宝直付通分账账号绑定 初始化请求参数:{}", JsonUtils.toJsonString(channelProfitAccountBindContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelProfitAccountBindContext.getMerchantAppChannelDO();
        // 是否普通商户
        boolean common = aliPayCommonParamsHandle.commonMer(merchantAppChannelDO);
        // 普通商户
        if(common){
            channelProfitAccountBindContext.setRemark("支付宝直付通分账不支持普通商户");
            channelProfitAccountBindContext.setProcessStatus(false);
            return;
        }
        // 支付宝直付通配置信息
        CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitAccountBindContext.getProviderChannelConfig().getParamsConfig());
        if(!alipayConfigCommonResult.isSuccess()){
            channelProfitAccountBindContext.setRemark(alipayConfigCommonResult.getMsg());
            channelProfitAccountBindContext.setProcessStatus(false);
            return;
        }
        // 分账信息
        MerchantFundAccountBindDO fundAccountBindDO = channelProfitAccountBindContext.getMerchantFundAccountBindDto().getMerchantFundAccountBindDO();
        try {
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfigCommonResult.getData());
            // 构造请求参数以调用接口
            AlipayTradeRoyaltyRelationBindModel model = new AlipayTradeRoyaltyRelationBindModel();
            model.setOutRequestNo(IdUtil.fastSimpleUUID());
            // 设置分账接收方列表
            RoyaltyEntity receiver = new RoyaltyEntity();
            receiver.setName(fundAccountBindDO.getName());
            receiver.setType("loginName");
            receiver.setAccount(fundAccountBindDO.getAccountNo());
            model.setReceiverList(CollUtil.newArrayList(receiver));

            AlipayTradeRoyaltyRelationBindRequest request = new AlipayTradeRoyaltyRelationBindRequest();
            request.setBizModel(model);

            log.info("支付宝直付通分账账号绑定 请求参数:{}", JSONUtil.toJsonStr(model));
            AlipayTradeRoyaltyRelationBindResponse response = client.execute(request);
            log.info("支付宝直付通分账账号绑定 返回参数:{}", JSONUtil.toJsonStr(response));

            if(response.isSuccess()){
                if(!response.getResultCode().equals("SUCCESS")){
                    channelProfitAccountBindContext.setProcessStatus(Boolean.FALSE);
                    channelProfitAccountBindContext.setRemark(response.getSubMsg());
                }
            }else{
                channelProfitAccountBindContext.setProcessStatus(Boolean.FALSE);
                channelProfitAccountBindContext.setRemark(response.getSubMsg());
            }
        }catch (Exception e) {
            log.error("支付宝直付通分账账号绑定,失败信息 message: {}", e);
            channelProfitAccountBindContext.setRemark("支付宝直付通分账账号绑定,失败信息" + e.getMessage());
            channelProfitAccountBindContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void unBindProfitAccount(ChannelProfitAccountBindContext channelProfitAccountBindContext) {
        log.info("支付宝直付通分账账号解绑 初始化请求参数:{}", JsonUtils.toJsonString(channelProfitAccountBindContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelProfitAccountBindContext.getMerchantAppChannelDO();
        // 是否普通商户
        boolean common = aliPayCommonParamsHandle.commonMer(merchantAppChannelDO);
        // 普通商户
        if(common){
            channelProfitAccountBindContext.setRemark("支付宝直付通分账不支持普通商户");
            channelProfitAccountBindContext.setProcessStatus(false);
            return;
        }
        // 支付宝直付通配置信息
        CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitAccountBindContext.getProviderChannelConfig().getParamsConfig());
        if(!alipayConfigCommonResult.isSuccess()){
            channelProfitAccountBindContext.setRemark(alipayConfigCommonResult.getMsg());
            channelProfitAccountBindContext.setProcessStatus(false);
            return;
        }
        // 分账信息
        MerchantFundAccountBindDO fundAccountBindDO = channelProfitAccountBindContext.getMerchantFundAccountBindDto().getMerchantFundAccountBindDO();
        try {
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfigCommonResult.getData());
            // 构造请求参数以调用接口
            AlipayTradeRoyaltyRelationUnbindModel model = new AlipayTradeRoyaltyRelationUnbindModel ();
            model.setOutRequestNo(IdUtil.fastSimpleUUID());
            // 设置分账接收方列表
            RoyaltyEntity receiver = new RoyaltyEntity();
            receiver.setName(fundAccountBindDO.getName());
            receiver.setType("loginName");
            receiver.setAccount(fundAccountBindDO.getAccountNo());
            model.setReceiverList(CollUtil.newArrayList(receiver));

            AlipayTradeRoyaltyRelationUnbindRequest request = new AlipayTradeRoyaltyRelationUnbindRequest();
            request.setBizModel(model);

            log.info("支付宝直付通分账账号解绑 请求参数:{}", JSONUtil.toJsonStr(model));
            AlipayTradeRoyaltyRelationUnbindResponse response = client.execute(request);
            log.info("支付宝直付通分账账号解绑 返回参数:{}", JSONUtil.toJsonStr(response));

            if(response.isSuccess()){
                if(!response.getResultCode().equals("SUCCESS")){
                    channelProfitAccountBindContext.setProcessStatus(Boolean.FALSE);
                    channelProfitAccountBindContext.setRemark(response.getSubMsg());
                }
            }else{
                channelProfitAccountBindContext.setProcessStatus(Boolean.FALSE);
                channelProfitAccountBindContext.setRemark(response.getSubMsg());
            }
        }catch (Exception e) {
            log.error("支付宝直付通分账账号解绑,失败信息 message: {}", e);
            channelProfitAccountBindContext.setRemark("支付宝直付通分账账号解绑,失败信息" + e.getMessage());
            channelProfitAccountBindContext.setProcessStatus(Boolean.FALSE);
        }

    }

    @Override
    public void queryAccountBindList(ChannelProfitAccountBindContext channelProfitAccountContext) {

    }
}
